<?php
/**
 * Set methods for use operation in dbtable.
 * 
 * This is a class generated with Zend_CodeGenerator.
 * 
 * @category Application
 * @package Application_User
 * @subpackage Model
 * @version $Id$
 * @license New BSD
 */


class User_Model_UsersMapper extends Core_Model_Mapper_Abstract
{

    protected $_dbName = 'User_Model_DbTable_Users';

    /**
     * check user login and password
     * 
     * @return bool
     */
    public function findUser(User_Model_Users $user)
    {
        $authAdapter = new Core_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
        $authAdapter->setTableName('users')
                    ->setIdentityColumn('login')
                    ->setCredentialColumn('password')
                    ->setCredentialTreatment('?');

        $authAdapter->setIdentity($user->getLogin())
                    ->setCredential($user->getPassword());

        $auth = Zend_Auth::getInstance();
        $auth->setStorage(new Zend_Auth_Storage_Session('Zend_Auth'));
        $result = $auth->authenticate($authAdapter);

        if ($result->isValid() === true) {
            $data = $authAdapter->getResultRowObject(null, 'password');
            $auth->getStorage()->write($data);
            return true;
        }
        return false;
    }

    /**
     * Registration new user
     *
     * @return bool
     */
    public function registrationUser(User_Model_Users $user)
    {
        $newRow = $this->getDbTable()->createRow();

        $newRow->login = $user->getLogin();
        $newRow->password = $user->getPassword();
        $newRow->role_id = $user->getRole_id();
        $newRow->create_date = $user->getCreate_date();
        $newRow->email = $user->getEmail();

        return $newRow->save();
    }

}

